
function [] = test_upsampling()

fsin = 6000; 
fsout = 30000; 
scale = fsout / fsin;

func = @(ti) sin(2*pi*650*ti-pi/4) + sin(2*pi*1500*ti+pi/2);
tend = 1/500*2;

% build input func.
Tin = 1/fsin; 
tin = (0:Tin:tend);
fin = func(tin);

% reconstruct
[fout, tout] = upsampling(fin, fsin, fsout);

% calculate ideal reconstruction
forg = func(tout);

fout1 = interp(fin, scale);
fout1 = fout1(1:1:end-scale+1);

% compare them

figure(1);
hold on;
plot(tout, forg, 'r');
plot(tin, fin, 'g');
plot(tout, fout, 'b');
plot(tout, fout1, 'm');
legend('ideal','in', 'out', 'interp');

% compare fq

FIN = fftshift(abs(fft(fin)));
FOUT = fftshift(abs(fft(fout)));
FOUT1 = fftshift(abs(fft(fout1)));
s = size(FIN, 2) / size(FOUT, 2);
FORG = s*fftshift(abs(fft(forg)));
FOUT = s*FOUT;
FOUT1 = s*FOUT1;

fqout = fsout/2*linspace(-1, 1, size(FORG, 2)); 
fqin = (1/Tin)/2*linspace(-1, 1, size(FIN, 2)); 

figure(2);
hold on;
plot(fqout, FORG, 'r');
plot(fqin, FIN, 'g');
plot(fqout, FOUT, 'b');
plot(fqout, FOUT1, 'm');
legend('ideal','in', 'out', 'interp');

%% error

error_calc(fout, forg);
error_calc(fout1, forg);

% time

fsin = 6000; 
fsout = 30000; 

func = @(ti) sin(2*pi*500*ti-pi/4) + sin(2*pi*2000*ti+pi/2);
tend = 30; % czym dluzsza tym lepsze widmo.

% build input func.
Tin = 1/fsin; 
tin = (0:Tin:tend);
fin = func(tin);

tic;

upsampling(fin, fsin, fsout);

display(sprintf('time: %d', toc))

end